package polarmaker.polars.smooth.gui.components;

import java.io.File;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.filechooser.FileFilter;

public class ToolFileFilter extends FileFilter
{
  private Hashtable<String, ToolFileFilter> filters = null;
  private String description = null;
  private String fullDescription = null;
  private boolean useExtensionsInDescription = true;

  public ToolFileFilter()
  {
    this((String) null, (String) null);
  }

  public ToolFileFilter(String extension)
  {
    this(extension, null);
  }

  public ToolFileFilter(String extension, String description)
  {
    this(new String[] {extension}, description);
  }

  public ToolFileFilter(String[] filters)
  {
    this(filters, null);
  }

  public ToolFileFilter(String[] filters, String description)
  {
    this.filters = new Hashtable<String, ToolFileFilter>(filters.length);
    for (int i = 0; i < filters.length; i++)
    {
      // add filters one by one
      addExtension(filters[i]);
    }
    setDescription(description);
  }

  public boolean accept(File f)
  {
    if(f != null)
    {
      if(f.isDirectory())
      {
        return true;
      }
      String extension = getExtension(f);
      if(extension != null && filters.get(getExtension(f)) != null)
      {
        return true;
      };
    }
    return false;
  }

  public String getExtension(File f)
  {
    if(f != null)
    {
      String filename = f.getName();
      int i = filename.lastIndexOf('.');
      if(i>0 && i<filename.length()-1)
      {
        return filename.substring(i+1).toLowerCase();
      };
    }
    return null;
  }

  public void addExtension(String extension)
  {
    if(filters == null)
    {
      filters = new Hashtable<String, ToolFileFilter>(5);
    }
    filters.put(extension.toLowerCase(), this);
    fullDescription = null;
  }

  public String getDescription()
  {
    if(fullDescription == null)
    {
      if(description == null || isExtensionListInDescription())
      {
        if(description != null)
        {
          fullDescription = description;
        }
        fullDescription += " (";
        // build the description from the extension list
        Enumeration extensions = filters.keys();
        if (extensions != null)
        {
          fullDescription += "." + (String) extensions.nextElement();
          while (extensions.hasMoreElements())
          {
            fullDescription += ", " + (String) extensions.nextElement();
          }
        }
        fullDescription += ")";
      }
      else
      {
        fullDescription = description;
      }
    }
    return fullDescription;
  }

  public void setDescription(String description)
  {
    this.description = description;
    fullDescription = null;
  }

  public void setExtensionListInDescription(boolean b)
  {
    useExtensionsInDescription = b;
    fullDescription = null;
  }

  public boolean isExtensionListInDescription()
  {
    return useExtensionsInDescription;
  }
}
